using System;
using UnityEngine;

public class LightningBolt : MonoBehaviour
{
	private Transform tr;

	public Transform target;

	public Transform tg;

	public int zigs = 100;

	public float speed = 1f;

	public float scale = 1f;

	public Transform start;

	public Transform end;

	private Perlin noise;

	private float oneOverZigs;

	private ParticleSystem.Particle[] particles;

	private ParticleSystem particleSystem;

	private void Start()
	{
		tr = base.transform;
		tg = GameObject.Find("knight dranzer").transform;
		oneOverZigs = 1f / (float)zigs;
		particleSystem = GetComponent<ParticleSystem>();
		var _temp_val_776 = particleSystem.emission; _temp_val_776.enabled = false;
		particles = new ParticleSystem.Particle[zigs];
		particleSystem.Emit(zigs);
		noise = new Perlin();
	}

	private void OnTriggerEnter(Collider other)
	{
		if ((bool)other.GetComponent<Rigidbody>() && other.GetComponent<Rigidbody>().angularVelocity.sqrMagnitude > 10f)
		{
			target = other.transform;
			other.GetComponent<Rigidbody>().angularDrag += 0.35f;
		}
	}

	private void OnTriggerExit(Collider other)
	{
		if ((bool)other.GetComponent<Rigidbody>())
		{
			other.GetComponent<Rigidbody>().angularDrag = Mathf.Clamp(other.GetComponent<Rigidbody>().angularDrag - 0.35f, 0f, 10f);
		}
	}

	private void Update()
	{
		tr.position = tg.position;
		float num = Time.time * speed * (154f / (415f * (float)Math.E));
		float num2 = Time.time * speed * 1.21688f;
		float num3 = Time.time * speed * 2.5564f;
		int num4 = particleSystem.GetParticles(particles);
		for (int i = 0; i < zigs; i++)
		{
			if ((bool)target)
			{
				Vector3 a = Vector3.Lerp(base.transform.position, target.position, oneOverZigs * (float)i);
				Vector3 a2 = new Vector3(noise.Noise(num + a.x, num + a.y, num + a.z), noise.Noise(num2 + a.x, num2 + a.y, num2 + a.z), noise.Noise(num3 + a.x, num3 + a.y, num3 + a.z)) * (8f / (1f + (float)i * 0.04f));
				a += a2 * scale * ((float)i * oneOverZigs);
				particles[i].position = a;
				particles[i].remainingLifetime = 1f;
			}
			else
			{
				target = base.transform;
			}
		}
		particleSystem.SetParticles(particles, particles.Length);
		end.position = particles[particles.Length - 1].position;
	}
}
